package com.hezhanying.controller;

import com.hezhanying.pojo.Emp;
import com.hezhanying.pojo.Result;
import com.hezhanying.service.EmpService;
import com.hezhanying.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;
    @PostMapping("/api/login")
    public Result login(@RequestBody Emp emp){
        Emp loginEmp = empService.login(emp);
        if(loginEmp!=null){
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("id",loginEmp.getId());
            claims.put("name",loginEmp.getName());
            claims.put("username",loginEmp.getUsername());
            String token = JwtUtils.generateJwt(claims);
            return Result.success(token);
        }
        return Result.error("用户名或密码错误");
    }

}
